package com.buba.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.mapper.InsuranceMapper;
import com.buba.service.InsuranceService;
import entity.salary.Insurance;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.salary.Social;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@Service
public class InsuranceServiceImpl extends ServiceImpl<InsuranceMapper, Insurance> implements InsuranceService {

    @Resource
    private InsuranceMapper insuranceMapper;

    /* *
     * @Author: beisheng
     * @Descriprion: 查询所有险种名称
     * @Date: 2023/3/16 16:19
     * @Return: common.R
     * @Param: []
     */

    @Override
    public List<String> selAllInsuName(Integer insuranceType) {
        List<Insurance> insurances = insuranceMapper.selectList(new QueryWrapper<Insurance>().eq("delete_flag", 0).eq("insurance_type",insuranceType));
        List<String> stringList = new ArrayList<>();
        for (Insurance insurance : insurances) {
            stringList.add(insurance.getInsuranceName());
        }
        return stringList;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据名称查找险种
     * @Date: 2023/3/16 16:51
     * @Return: common.R
     * @Param: [allInsuName]
     */

    @Override
    public List<Insurance> selAllInsuces(List<String> allInsuName, Integer insuranceType) {
        List<Insurance> insuranceList = insuranceMapper.selectList(new QueryWrapper<Insurance>().eq("delete_flag",0).in("insurance_name",allInsuName).eq("insurance_type",insuranceType));
        return insuranceList;
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加社保险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @Override
    public void saveSocialToInsurance(Map<String, Object> map) {
        Insurance social = (Insurance) map.get("social");
        social.setInsuranceType(1);
        social.setCreateTime(new Date());
        social.setUpdateTime(new Date());
        social.setDeleteFlag(0);
        insuranceMapper.insert(social);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 添加公积金险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @Override
    public void saveAccumToInsurance(Map<String, Object> map) {
        Insurance accum = (Insurance) map.get("accum");
        accum.setInsuranceType(2);
        accum.setCreateTime(new Date());
        accum.setUpdateTime(new Date());
        accum.setDeleteFlag(0);
        insuranceMapper.insert(accum);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改社保险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @Override
    public void updateSocialToInsurance(Map<String, Object> map) {
        Insurance social = (Insurance) map.get("social");
        social.setUpdateTime(new Date());
        insuranceMapper.update(social,new QueryWrapper<Insurance>().eq("delete_flag",0).eq("sa_id",social.getSaId()));
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 修改公积金险种
     * @Date: 2023/3/21 20:14
     * @Return: common.R
     * @Param: [map]
     */

    @Override
    public void updateAccumToInsurance(Map<String, Object> map) {
        Insurance accum = (Insurance) map.get("accum");
        accum.setUpdateTime(new Date());
        insuranceMapper.update(accum,new QueryWrapper<Insurance>().eq("delete_flag",0).eq("sa_id",accum.getSaId()));
    }

    @Override
    public List<Insurance> getInsurance(List<String> checkedCities) {
        List<Insurance> insuranceList = insuranceMapper.selectList(new QueryWrapper<Insurance>().in("insurance_name", checkedCities));
        return insuranceList;
    }
}
